x = int(input())
f = []
f.append(x)
x = str(x)
d = []
if 1000 > int(x) or int(x) > 9999:
    print(0)
    exit()

for i in range(4):
    f.append(int(x[i]))

    if i != 3:
        f.append(int(x[i:i+2]))

    if i != 2:
        f.append(int(x[i:i + 3]))
for i in f:

    for j in range(2, i):
        if i % j == 0:
            break
    else:
        d.append(i)

if d == []:
    print(0)
    exit()
else:
    print(max(d))